package com.datastructure.test.ispailoflistnode;

public class IsPailOfListNode {

    public static void main(String[] args) {
        ListNode listNode = new ListNode(1);
        listNode.next=new ListNode(2);
        listNode.next.next=new ListNode(1);
        listNode.next.next.next=new ListNode(1);
        IsPailOfListNode isPailOfListNode = new IsPailOfListNode();
        boolean pail = isPailOfListNode.isPail(listNode);
        System.out.println(pail);
    }

    public boolean isPail (ListNode head) {
        // write code here
          ListNode cur = null;
          ListNode cur1 = null;
         int lengthOfListNode = getLengthOfListNode(head);
         if(lengthOfListNode==1){
             return true;
         }
         int mid = lengthOfListNode/2;
         for (int i = 0; i < mid; i++) {
            int j =0;
            cur = head;
            cur1 = head;
            while (j<i){
                cur=cur.next;
                j++;
            }
            int k = lengthOfListNode-i-1;
            while (k>0){
                cur1=cur1.next;
                k--;
            }
            if(cur.val!= cur1.val){
                return false;
            }
         }
         return true;
    }

    public int getLengthOfListNode(ListNode head){
        ListNode cur = head;
        int length=0;
        while (cur!=null){
            length++;
            cur=cur.next;
        }
        return length;
    }

    private ListNode reverse(ListNode head) {
        ListNode res = null;
        ListNode cur = head;
        while (cur!=null){
            ListNode temp = cur.next;
            cur.next=res;
            res = cur;
            //这里赋值了，所以head值也就发生变化了
            cur=temp;
        }
        return res;
    }
}

class ListNode {
   int val;
   ListNode next = null;

    public ListNode(int val) {
        this.val = val;
    }
}
